<?
require_once('class/conexion/conexion.php');


class DBAdmin
{

	public $mConexion = null;

	public function __construct() 
	{
		$this->mConexion = new Conexion($this);
	}

	public function instalar() 
	{
		//Tabla RED_SOCIAL
		
		$sql[] ="DROP TABLE IF EXISTS RED_SOCIAL";

		$sql[] ="CREATE TABLE RED_SOCIAL
				(ID INT NOT NULL AUTO_INCREMENT, 
				NOMBRE VARCHAR(50) NOT NULL, 
				TIEMPO INT NOT NULL DEFAULT 0, 
				PRIMARY KEY(ID))";

		
		//Tabla ATRIBUTO
				
		$sql[] = "DROP TABLE IF EXISTS ATRIBUTO";

		$sql[] = "CREATE TABLE ATRIBUTO
				(ID INT NOT NULL AUTO_INCREMENT, 
				NOMBRE VARCHAR(50) NOT NULL, 
				TIPO VARCHAR(20), 
				PRIMARY KEY(ID),
				FOREIGN KEY (TIPO) REFERENCES TIPO(ID))";		


		//Tabla ATRIBUTOS_RED
		
		$sql[] = "DROP TABLE IF EXISTS ATRIBUTOS_RED";

		$sql[] = "CREATE TABLE ATRIBUTOS_RED
				(ID_RED INT, 
				ID_ATRIBUTO INT,  
				TIPO VARCHAR(20) NOT NULL, 
				PRIMARY KEY(ID_RED, ID_ATRIBUTO, TIPO),
				FOREIGN KEY(ID_RED) REFERENCES RED_SOCIAL(ID) ON DELETE CASCADE,
				FOREIGN KEY (ID_ATRIBUTO) REFERENCES ATRIBUTO(ID))";


		//Tabla NODO
		
		$sql[] = "DROP TABLE IF EXISTS NODO";

		$sql[] = "CREATE TABLE NODO
				(ID INT NOT NULL AUTO_INCREMENT, 
				ID_RED INT NOT NULL, 
				NOMBRE VARCHAR(20) NOT NULL, 
	    		BORRADO BOOL NOT NULL DEFAULT FALSE, 
	    		TIEMPO INT NOT NULL, 
				PRIMARY KEY(ID, ID_RED, NOMBRE, TIEMPO),
				FOREIGN KEY(ID_RED) REFERENCES RED_SOCIAL(ID) ON DELETE CASCADE)";


		//Tabla ENLACE
		
		$sql[] = "DROP TABLE IF EXISTS ENLACE";

		$sql[] = "CREATE TABLE ENLACE
				(ID INT NOT NULL AUTO_INCREMENT,
				ID_ORIGEN INT NOT NULL, 
				ID_DESTINO INT NOT NULL, 
				TIEMPO INT NOT NULL, 
				PRIMARY KEY(ID, ID_ORIGEN, ID_DESTINO, TIEMPO),
				FOREIGN KEY(ID_ORIGEN) REFERENCES NODO(ID) ON DELETE CASCADE,
				FOREIGN KEY(ID_DESTINO) REFERENCES NODO(ID) ON DELETE CASCADE)";


		//Tabla VALOR_ATRIBUTO

		$sql[] = "DROP TABLE IF EXISTS VALOR_ATRIBUTO";

		$sql[] = "CREATE TABLE VALOR_ATRIBUTO
				(ID_ORIGEN INT, 
				TIPO_ORIGEN VARCHAR(10), 
				ID_ATRIBUTO INT, 
				VALOR VARCHAR(50), 
				TIEMPO INT NOT NULL,
	    		PRIMARY KEY(ID_ORIGEN, TIPO_ORIGEN, ID_ATRIBUTO, TIEMPO),
				FOREIGN KEY(ID_ORIGEN) REFERENCES NODO(ID) ON DELETE CASCADE,
				FOREIGN KEY(ID_ATRIBUTO) REFERENCES ATRIBUTO(ID))";
		
		
		//Tabla VALOR_LISTA

		$sql[] = "DROP TABLE IF EXISTS VALOR_LISTA";

		$sql[] = "CREATE TABLE VALOR_LISTA
				(ID INT AUTO_INCREMENT, 
				TIPO INT, 
				VALOR VARCHAR(50), 
	    		PRIMARY KEY(ID))";
			
		//insertamos valores de ejemplo en este caso los dias de la semana.
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (3, 'Lunes')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (3, 'Martes')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (3, 'Miercoles')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (3, 'Jueves')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (3, 'Viernes')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (3, 'Sabado')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (4, 'Hombre')";
		$sql[] ="INSERT INTO VALOR_LISTA (TIPO, VALOR) VALUES (4, 'Mujer')";
			
		//Tabla TIPO
		$sql[] = "DROP TABLE IF EXISTS TIPO";

		$sql[] = "CREATE TABLE TIPO
				(ID INT NOT NULL AUTO_INCREMENT,
				NOMBRE VARCHAR(20) NOT NULL,
				VALOR VARCHAR(20) NOT NULL,
	    		PRIMARY KEY(ID))";
			
		
		$sql[] ="INSERT INTO TIPO (NOMBRE ,VALOR) VALUES ('Entero',  'Int')";
		$sql[] ="INSERT INTO TIPO (NOMBRE ,VALOR) VALUES ('Cadena',  'varchar(30)')";
		$sql[] ="INSERT INTO TIPO (NOMBRE ,VALOR) VALUES ('Dias',  'Lista')";
		$sql[] ="INSERT INTO TIPO (NOMBRE ,VALOR) VALUES ('Sexo',  'Lista')";
		

		$this->mConexion->sql($sql);
	}
	
}

?>
